const http = require('http');
const fs = require('fs');
const path = require('path');

const PORT = 8081;
const IMAGE_PATH = path.join(__dirname, '462255af4177d2176c1dd19325351af1.png');

const server = http.createServer((req, res) => {
  if (req.url === '/') {
    try {
      // 读取图片并转换为Base64
      const imageBuffer = fs.readFileSync(IMAGE_PATH);
      const base64Image = imageBuffer.toString('base64');
      const dataUri = "data:image/png;base64," + base64Image;

      // 返回包含图片的HTML
      res.writeHead(200, {'Content-Type': 'text/html'});
      res.end(
        "<!DOCTYPE html>" +
        "<html>" +
        "<head>" +
          "<title>Base64 Image Example</title>" +
        "</head>" +
        "<body>" +
          "<h1>Base64 Encoded Image</h1>" +
          "<img src=\"" + dataUri + "\" alt=\"Base64 Image\">" +
        "</body>" +
        "</html>"
      );
    } catch (err) {
      res.writeHead(500);
      res.end('Error loading image');
      console.error('Error:', err);
    }
  } else {
    res.writeHead(404);
    res.end('Not Found');
  }
});

server.listen(PORT, () => {
  console.log("Server running at http://localhost:" + PORT);
});